<?php
class Inout_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_outgoing_incoming.oper_date, \'%d.%m.%Y\') as oper_date, 
							_outgoing_incoming._filials_rid as _filials_rid,
							_outgoing_incoming._currencies_rid as _currencies_rid,
							_outgoing_incoming._account_states_rid as _account_states_rid,
							_outgoing_incoming.summ_value as summ_value,
							_filials.name as filial_name,
							_currencies.code as currency_code,
							_account_states.state_name as state_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_outgoing_incoming', '_outgoing_incoming._documents_rid = _documents.rid');
		$this->db->join('_filials', '_filials.rid = _outgoing_incoming._filials_rid');
		$this->db->join('_currencies', '_currencies.rid = _outgoing_incoming._currencies_rid');
		$this->db->join('_account_states', '_account_states.rid = _outgoing_incoming._account_states_rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$whereArr = array();
			if(isset($searchRule['_outgoing_incoming.oper_date>='])) { $whereArr['_outgoing_incoming.oper_date>='] = $this->toMySQLDate($searchRule['_outgoing_incoming.oper_date>=']); unset($searchRule['_outgoing_incoming.oper_date>=']); } 
			if(isset($searchRule['_outgoing_incoming.oper_date<='])) { $whereArr['_outgoing_incoming.oper_date<='] = $this->toMySQLDate($searchRule['_outgoing_incoming.oper_date<=']); unset($searchRule['_outgoing_incoming.oper_date<=']); } 
			if(isset($searchRule['_outgoing_incoming.summ_value>='])) { $whereArr['_outgoing_incoming.summ_value>='] = $this->toMySQLDate($searchRule['_outgoing_incoming.summ_value>=']); unset($searchRule['_outgoing_incoming.summ_value>=']); } 
			if(isset($searchRule['_outgoing_incoming.summ_value<='])) { $whereArr['_outgoing_incoming.summ_value<='] = $this->toMySQLDate($searchRule['_outgoing_incoming.summ_value<=']); unset($searchRule['_outgoing_incoming.summ_value<=']); } 
			//var_dump($searchRule);
			if($whereArr)  $this->db->where($whereArr);
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetCurrenciesList(){
		$this->db->select('_currencies.rid, _currencies.code as currency_code');
		$this->db->from('_currencies');
		$this->db->orderby('currency_code');
		$query = $this->DBGet('_currencies');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetFilialsList(){
		$this->db->select('_filials.rid, _filials.name as filial_name');
		$this->db->from('_filials');
		$this->db->orderby('filial_name');
		$query = $this->DBGet('_filials');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetAccountStatesList(){
		$this->db->select('_account_states.rid, _account_states.state_name as state_name');
		$this->db->from('_account_states');
		$this->db->orderby('state_name');
		$query = $this->DBGet('_account_states');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_outgoing_incoming.oper_date, \'%d.%m.%Y\') as oper_date, 
							_outgoing_incoming._filials_rid as _filials_rid,
							_outgoing_incoming._currencies_rid as _currencies_rid,
							_outgoing_incoming._account_states_rid as _account_states_rid,
							_outgoing_incoming.summ_value as summ_value,
							_filials.name as filial_name,
							_currencies.code as currency_code,
							_account_states.state_name as state_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_outgoing_incoming', '_outgoing_incoming._documents_rid = _documents.rid');
		$this->db->join('_filials', '_filials.rid = _outgoing_incoming._filials_rid');
		$this->db->join('_currencies', '_currencies.rid = _outgoing_incoming._currencies_rid');
		$this->db->join('_account_states', '_account_states.rid = _outgoing_incoming._account_states_rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('_documents_rid'=>$docRid,
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'_filials_rid'=>$_POST['_filials_rid'],
							'_account_states_rid'=>$_POST['_account_states_rid'],
							'oper_date'=>$this->toMySQLDate($_POST['oper_date']),
							'summ_value'=>$_POST['summ_value'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_outgoing_incoming', $insHeaderArray);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array('_currencies_rid'=>$_POST['_currencies_rid'],
							'_filials_rid'=>$_POST['_filials_rid'],
							'_account_states_rid'=>$_POST['_account_states_rid'],
							'summ_value'=>$_POST['summ_value'],
							'oper_date'=>$this->toMySQLDate($_POST['oper_date']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->update('_outgoing_incoming', $updateHeaderArray, array('_outgoing_incoming._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>